<template>
  <div class="settings p-4">
    <h1 class="text-2xl font-bold mb-4">Settings</h1>

    <div class="bg-white rounded-lg shadow p-4 mb-4">
      <h2 class="text-xl font-bold mb-4">API Configuration</h2>
      <form @submit.prevent="saveAPISettings" class="space-y-4">
        <div>
          <label for="apiKey" class="block text-sm font-medium text-gray-700">API Key</label>
          <input type="text" id="apiKey" v-model="apiSettings.apiKey" 
                 class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" />
        </div>
        <div>
          <label for="apiEndpoint" class="block text-sm font-medium text-gray-700">API Endpoint</label>
          <input type="text" id="apiEndpoint" v-model="apiSettings.apiEndpoint" 
                 class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" />
        </div>
        <div>
          <button type="submit" 
                  class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
            Save API Settings
          </button>
        </div>
      </form>
    </div>

    <div class="bg-white rounded-lg shadow p-4 mb-4">
      <h2 class="text-xl font-bold mb-4">Model Training Settings</h2>
      <form @submit.prevent="saveModelSettings" class="space-y-4">
        <div>
          <label for="batchSize" class="block text-sm font-medium text-gray-700">Batch Size</label>
          <input type="number" id="batchSize" v-model="modelSettings.batchSize" 
                 class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" />
        </div>
        <div>
          <label for="epochs" class="block text-sm font-medium text-gray-700">Epochs</label>
          <input type="number" id="epochs" v-model="modelSettings.epochs" 
                 class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" />
        </div>
        <div>
          <label for="learningRate" class="block text-sm font-medium text-gray-700">Learning Rate</label>
          <input type="number" id="learningRate" v-model="modelSettings.learningRate" step="0.001" 
                 class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" />
        </div>
        <div>
          <button type="submit" 
                  class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
            Save Model Settings
          </button>
        </div>
      </form>
    </div>

    <div class="bg-white rounded-lg shadow p-4">
      <h2 class="text-xl font-bold mb-4">Data Sources</h2>
      <div class="space-y-4">
        <div v-for="(source, index) in dataSources" :key="index" class="flex justify-between items-center">
          <div>
            <h3 class="font-bold">{{ source.name }}</h3>
            <p class="text-sm text-gray-600">{{ source.description }}</p>
          </div>
          <div class="flex items-center">
            <span class="mr-2">Enabled</span>
            <label class="switch">
              <input type="checkbox" v-model="source.enabled">
              <span class="slider round"></span>
            </label>
          </div>
        </div>
        <button @click="saveDataSources" 
                class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
          Save Data Sources
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useAITradeStore } from '../store'

const store = useAITradeStore()

const apiSettings = ref({
  apiKey: '',
  apiEndpoint: ''
})

const modelSettings = ref({
  batchSize: 32,
  epochs: 100,
  learningRate: 0.001
})

const dataSources = ref([
  { name: 'Akshare', description: 'Chinese stock market data', enabled: true },
  { name: 'Alpha Vantage', description: 'US stock market data', enabled: false },
  { name: 'Yahoo Finance', description: 'Global market data', enabled: true },
  { name: 'News API', description: 'Financial news and sentiment', enabled: true }
])

onMounted(async () => {
  await loadSettings()
})

async function loadSettings() {
  try {
    const settings = await store.getSettings()
    
    if (settings.api) {
      apiSettings.value = settings.api
    }
    
    if (settings.model) {
      modelSettings.value = settings.model
    }
    
    if (settings.dataSources) {
      dataSources.value = settings.dataSources
    }
  } catch (error) {
    console.error('Error loading settings:', error)
  }
}

async function saveAPISettings() {
  try {
    await store.saveSettings({ api: apiSettings.value })
    alert('API settings saved successfully')
  } catch (error) {
    console.error('Error saving API settings:', error)
    alert('Failed to save API settings')
  }
}

async function saveModelSettings() {
  try {
    await store.saveSettings({ model: modelSettings.value })
    alert('Model settings saved successfully')
  } catch (error) {
    console.error('Error saving model settings:', error)
    alert('Failed to save model settings')
  }
}

async function saveDataSources() {
  try {
    await store.saveSettings({ dataSources: dataSources.value })
    alert('Data sources settings saved successfully')
  } catch (error) {
    console.error('Error saving data sources:', error)
    alert('Failed to save data sources')
  }
}
</script>

<style scoped>
.settings {
  @apply min-h-screen bg-gray-100;
}

/* Toggle Switch Styling */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
</style>